package cn.ym.mall.dao.impl;

import cn.ym.mall.dao.GoodsDao;
import cn.ym.mall.entity.Goods;
import cn.ym.mall.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 商品dao实现类
 */
public class GoodsDaoImpl implements GoodsDao {
    QueryRunner queryRunner = new QueryRunner();

    /**
     * 分页查询所有商品
     *
     * @return
     * @throws SQLException
     */
    @Override
    public List<Goods> getAll() throws SQLException {

        List<Goods> goodsList = new ArrayList<>();
        try {
            String sql = "select gid, sid, bid, tid, gname, gcost, gprice, gcontent, gputawayDate, gputawayState, ginventory, gpicturePath, gcheckState, gcolor, gsellCount from ym_goods";
            goodsList = queryRunner.query(DruidUtils.getConnection(),sql,new BeanListHandler<Goods>(Goods.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return goodsList;
    }

    /**
     * 分页查询所有商品
     *
     * @param pageNo
     * @param pageSize
     * @return
     * @throws SQLException
     */
    @Override
    public List<Goods> getAll(Integer pageNo, Integer pageSize) throws SQLException {
        List<Goods> goodsList = new ArrayList<>();
        try {
            String sql = "select gid, sid, bid, tid, gname, gcost, gprice, gcontent, gputawayDate, gputawayState, ginventory, gpicturePath, gcheckState, gcolor, gsellCount from ym_goods order by gsellCount desc limit ?,?;";
            goodsList = queryRunner.query(DruidUtils.getConnection(),sql,new BeanListHandler<Goods>(Goods.class),(pageNo-1)*pageSize,pageSize);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return goodsList;
    }

    /**
     * 查询商品数
     *
     * @return
     */
    @Override
    public long getCount() throws SQLException {
        long count = 0;
        try {
            String sql = "select count(0) from ym_goods;";
            count = queryRunner.query(DruidUtils.getConnection(),sql,new ScalarHandler<Long>());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }

    /**
     * 分页查询分类所有商品
     *
     * @param bid
     * @param pageNo
     * @param pageSize
     * @return
     * @throws SQLException
     */
    @Override
    public List<Goods> getGoodsByBid(Integer bid, Integer pageNo, Integer pageSize) throws SQLException {
        List<Goods> goodsList = new ArrayList<>();
        try {
            String sql = "select gid, sid, bid, tid, gname, gcost, gprice, gcontent, gputawayDate, gputawayState, ginventory, gpicturePath, gcheckState, gcolor, gsellCount from ym_goods where bid = ? order by gsellCount desc limit ?,?;";
            goodsList = queryRunner.query(DruidUtils.getConnection(),sql,new BeanListHandler<Goods>(Goods.class),bid,(pageNo-1)*pageSize,pageSize);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return goodsList;
    }

    /**
     * 查询分类商品数
     *
     * @param bid
     * @return
     */
    @Override
    public long getCountByBid(Integer bid) throws SQLException {
        long count = 0;
        try {
            String sql = "select count(0) from ym_goods where bid = ?;";
            count = queryRunner.query(DruidUtils.getConnection(),sql,new ScalarHandler<Long>(),bid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }

    /**
     * 分页查询品牌所有商品
     *
     * @param tid
     * @param pageNo
     * @param pageSize
     * @return
     * @throws SQLException
     */
    @Override
    public List<Goods> getGoodsByTid(Integer tid, Integer pageNo, Integer pageSize) throws SQLException {
        List<Goods> goodsList = new ArrayList<>();
        try {
            String sql = "select gid, sid, bid, tid, gname, gcost, gprice, gcontent, gputawayDate, gputawayState, ginventory, gpicturePath, gcheckState, gcolor, gsellCount from ym_goods where tid = ? order by gsellCount desc limit ?,?;";
            goodsList = queryRunner.query(DruidUtils.getConnection(),sql,new BeanListHandler<Goods>(Goods.class),tid,(pageNo-1)*pageSize,pageSize);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return goodsList;
    }

    /**
     * 查询品牌商品数
     *
     * @param tid
     * @return
     */
    @Override
    public long getCountByTid(Integer tid) throws SQLException {
        long count = 0;
        try {
            String sql = "select count(0) from ym_goods where tid = ?;";
            count = queryRunner.query(DruidUtils.getConnection(),sql,new ScalarHandler<Long>(),tid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }

    /**
     * 分页查询商店所有商品
     *
     * @param sid
     * @param pageNo
     * @param pageSize
     * @return
     * @throws SQLException
     */
    @Override
    public List<Goods> getGoodsBySid(Integer sid, Integer pageNo, Integer pageSize) throws SQLException {
        List<Goods> goodsList = new ArrayList<>();
        try {
            String sql = "select gid, sid, bid, tid, gname, gcost, gprice, gcontent, gputawayDate, gputawayState, ginventory, gpicturePath, gcheckState, gcolor, gsellCount from ym_goods where sid = ? order by gsellCount desc limit ?,?;";
            goodsList = queryRunner.query(DruidUtils.getConnection(),sql,new BeanListHandler<Goods>(Goods.class),sid,(pageNo-1)*pageSize,pageSize);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return goodsList;
    }

    /**
     * 查询商店商品数
     *
     * @param sid
     * @return
     */
    @Override
    public long getCountBySid(Integer sid) throws SQLException {
        long count = 0;
        try {
            String sql = "select count(0) from ym_goods where sid = ?;";
            count = queryRunner.query(DruidUtils.getConnection(),sql,new ScalarHandler<Long>(),sid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }

    /**
     * 通过名字模糊查询商品
     *
     * @param name
     * @return
     */
    @Override
    public List<Goods> getGoodsBySname(String name, Integer pageNo, Integer pageSize) {
        List<Goods> goodsList = new ArrayList<>();
        try {
            String sql = "select gid, sid, bid, tid, gname, gcost, gprice, gcontent, gputawayDate, gputawayState, ginventory, gpicturePath, gcheckState, gcolor, gsellCount from ym_goods where gname like order by gsellCount desc limit ?,?;";
            goodsList = queryRunner.query(DruidUtils.getConnection(),sql,new BeanListHandler<Goods>(Goods.class),"%"+name+"%",(pageNo-1)*pageSize,pageSize);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return goodsList;
    }

    /**
     * 通过名字模糊查询商品
     *
     * @param name
     * @return
     */
    @Override
    public long getCountBySname(String name) throws SQLException {
        long count = 0;
        try {
            String sql = "select count(0) from ym_goods where gname = ?;";
            count = queryRunner.query(DruidUtils.getConnection(),sql,new ScalarHandler<Long>(),"%"+name+"%");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }

    /**
     * 通过id查商品
     * @param gid
     * @return
     * @throws SQLException
     */
    @Override
    public Goods getGoodsByGid(Integer gid) throws SQLException {
        Goods goods = null;
        try {
            String sql = "select gid, sid, bid, tid, gname, gcost, gprice, gcontent, gputawayDate, gputawayState, ginventory, gpicturePath, gcheckState, gcolor, gsellCount from ym_goods where gid = ?;";
            goods = queryRunner.query(DruidUtils.getConnection(),sql,new BeanHandler<Goods>(Goods.class),gid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return goods;
    }

    /**
     * 添加商品
     *
     * @param goods
     * @return
     */
    @Override
    public int add(Goods goods) {
        int num = 0;
        try {
            String sql = "insert into ym_goods(sid, bid, tid, gname, gcost, gprice, gputawayDate, gputawayDate, gputawayState, ginventory, gpicturePath, gcheckState, gcolor,gsellCount) value (?,?,?,?,?,?,?,?,?,?,?,?,?);";
            num = queryRunner.update(DruidUtils.getConnection(),sql,goods.getSid(),goods.getBid(),goods.getTid(),goods.getGname(),goods.getGcost(),goods.getGprice(),goods.getGcontent(),goods.getGputawayDate(),goods.getGputawayState(),goods.getGinventory(),goods.getGpicturePath(),goods.getGcheckState(),goods.getGcolor(),goods.getGsellCount());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return num;
    }

    /**
     * 修改商品
     *
     * @param goods
     * @return
     */
    @Override
    public int update(Goods goods) {
        int num = 0;
        try {
            String sql = "update ym_goods set sid = ?, bid = ?, tid = ?, gname = ?, gcost = ?, gprice = ?, gcontent = ?, gputawayDate = ?, gputawayState = ?, ginventory = ?, gpicturePath = ?, gcheckState = ?, gcolor = ?, gsellCount = ? where gid = ?;";
            num = queryRunner.update(DruidUtils.getConnection(),sql,goods.getSid(),goods.getBid(),goods.getTid(),goods.getGname(),goods.getGcost(),goods.getGprice(),goods.getGcontent(),goods.getGputawayDate(),goods.getGputawayState(),goods.getGinventory(),goods.getGpicturePath(),goods.getGcheckState(),goods.getGcolor(),goods.getGsellCount(),goods.getGid());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return num;
    }

    /**
     * 删除商品
     *
     * @param gid
     * @return
     */
    @Override
    public int delete(Integer gid) {
        int num = 0;
        try {
            String sql = "delete from ym_goods where gid = ?;";
            num = queryRunner.update(DruidUtils.getConnection(),sql,gid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return num;
    }

}
